package com.assistant.command.impl;

import com.assistant.command.Command;
import com.assistant.core.CommandData;
import com.assistant.system.SystemUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Anyang
 * @version 1.0.0
 * @date 2025/10/25 05:10:24
 * @description 创建一个打开网址的命令
 */
public class OpenUrlCommand implements Command {
    private static final Logger logger = LoggerFactory.getLogger(OpenUrlCommand.class);

    @Override
    public boolean supports(String action) {
        return "open_url".equals(action.toLowerCase());
    }

    @Override
    public String execute(CommandData data) {
        String targetUrl = data.getTarget();
        logger.info("执行指令：打开网址 -> {}", targetUrl);

        if (targetUrl.isEmpty()) {
            return "请提供一个有效的网址，例如 '打开百度首页'。";
        }

        boolean success = SystemUtil.openUrl(targetUrl);

        if (success) {
            return String.format("好的，已为您打开网址【%s】。", targetUrl);
        } else {
            // 注意：OpenAppCommand 的纠错逻辑已确保传入的 targetUrl 是一个网址
            return String.format("抱歉，我未能成功打开网址【%s】，请检查网络或浏览器设置。", targetUrl);
        }
    }
}